import 'package:erp_widget/src/theme/index.dart';
import 'package:flutter/material.dart';

import 'content.dart';

class DxFillImageCard extends StatelessWidget {
  const DxFillImageCard({
    Key? key,
    this.width,
    this.height,
    this.imageHeight,
    this.borderRadius = 6,
    this.contentPadding,
    this.tags,
    this.title,
    this.description,
    this.footer,
    this.color = Colors.white,
    this.tagSpacing,
    this.tagRunSpacing,
    this.src,
  }) : super(key: key);

  /// card width
  final double? width;

  /// card height
  final double? height;

  /// image height
  final double? imageHeight;

  /// border radius value
  final double borderRadius;

  /// spacing between tag
  final double? tagSpacing;

  /// run spacing between line tag
  final double? tagRunSpacing;

  /// content padding
  final EdgeInsets? contentPadding;

  /// list of widgets
  final List<Widget>? tags;

  /// card color
  final Color color;

  /// widget title of card
  final Widget? title;

  /// widget description of card
  final Widget? description;

  /// widget footer of card
  final Widget? footer;

  /// 图片
  final String? src;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(borderRadius), color: color),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          ClipRRect(
            borderRadius:
            BorderRadius.only(topLeft: Radius.circular(borderRadius), topRight: Radius.circular(borderRadius)),
            child: _buildImage(),
          ),
          DxImageCardContent(
            contentPadding: contentPadding,
            tags: tags,
            title: title,
            footer: footer,
            description: description,
            tagSpacing: tagSpacing,
            tagRunSpacing: tagRunSpacing,
          ),
        ],
      ),
    );
  }

  /// 构建图片Loading图标
  Widget _buildLoading() {
    return SizedBox(
      width: width,
      height: imageHeight,
      child: const SizedBox(
        width: 20,
        height: 20,
        child: CircularProgressIndicator(color: DxStyle.gray3, strokeWidth: 1),
      ),
    );
  }

  Widget _buildError() {
    return Image.asset(
      DxAsset.defaultImage,
      width: width,
      height: imageHeight,
      fit: BoxFit.cover,
      package: 'erp_widget',
    );
  }

  /// 构建图片内容
  Widget _buildImage() {
    if (src == null || src == '') {
      return Image.asset(DxAsset.defaultImage,
          width: width, height: imageHeight, fit: BoxFit.cover, package: 'erp_widget');
    }

    final bool isNetwork = RegExp('^https?://').hasMatch(src!);

    if (isNetwork) {
      return Image.network(
        src!,
        width: width,
        height: imageHeight,
        loadingBuilder: (_, Widget child, ImageChunkEvent? loadingProgress) {
          if (src != null && src!.isNotEmpty && loadingProgress == null) {
            return child;
          }

          return _buildLoading();
        },
        errorBuilder: (_, Object error, StackTrace? stackTrace) {
          return _buildError();
        },
        fit: BoxFit.cover,
      );
    }

    return Image.asset(src!, width: width, height: height, fit: BoxFit.cover);
  }
}
